package com.web.util.obj.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.struts.util.LabelValueBean;

import com.web.util.sql.DatabaseConnector;

public class CountryListDao {
	
	public static ArrayList<LabelValueBean> getCountries() {
		
		PreparedStatement pStat = null;
		ResultSet rs = null;
		String sql = null;
		
		ArrayList<LabelValueBean> countries = null;
		
		sql = Messages.getString("CountryListDao.sql.select"); //$NON-NLS-1$
		try {
			pStat = DatabaseConnector.getConnection().prepareStatement(sql);
			rs = pStat.executeQuery();
			
			while (rs.next()) {
				String code = rs.getString(Messages.getString("CountryListDao.columns.id")); //$NON-NLS-1$
				String name = rs.getString(Messages.getString("CountryListDao.columns.name")); //$NON-NLS-1$
				LabelValueBean country = new LabelValueBean(name, code);
				if (countries==null) {
					countries = new ArrayList<LabelValueBean>(10);
				}
				countries.add(country);
			}			
			return countries;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return null;		
	}
	
	public static String getCountryName(String id) {
		
		String name = null;
		PreparedStatement pStat = null;
		ResultSet rs = null;
		String sql = null;
		
		sql = Messages.getString("CountryListDao.sql.selectName"); //$NON-NLS-1$
		
		try {
			pStat = DatabaseConnector.getConnection().prepareStatement(sql);
			pStat.setString(1, id);
			rs = pStat.executeQuery();
			
			if (rs.next()) {
				if (rs!=null) {
					name = rs.getString("name");
				}
			}			
			return name;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return null;
	}
	
}
